<template>
     <!-- 整体容器 -->
     <div class="pages">
        <el-container class="page-container">
            <!-- 搜索 -->
            <el-header class="page-header">
                <el-input style="width:200px;margin-right: 20px;" v-model.trim="refInput" placeholder="请输入用户ID" clearable/>
                <el-button type="primary" :icon="Search" @click="handlerFind">查找</el-button>
            </el-header>
            <el-main class="page-main">
                <!-- 表格 -->
                <div class="main-table">
                    <el-table style="width: 100%" :data="tableData" border >
                        <el-table-column prop="id" label="地址ID" align="center" width='70px'/>
                        <el-table-column prop="userId" label="用户ID" align="center" width='70px'/>
                        <el-table-column prop="consignee" label="签收人" align="center" width='120px'/>
                        <el-table-column prop="phone" label="手机号" align="center" width='140px'/>
                        <el-table-column prop="province" label="省份" align="center"/>
                        <el-table-column prop="city" label="城市" align="center"/>
                        <el-table-column prop="county" label="区/县" align="center"/>
                        <el-table-column prop="address" label="详情地址" align="center"/>
                    </el-table>
                </div>
                <!-- 分页组件 -->
                <div class="page-pagination">
                    <Pagination v-bind="addressPagination" @handle-pagination="emitEntProps"/>
                </div>
                <!-- 回到顶部组件 -->
                <el-backtop :right="100" :bottom="100" target=".el-main" ref="refBacktop" />
            </el-main>
        </el-container>
    </div>
</template>

<script lang="ts" setup>
import { ClassPagination } from '#/index';
import { reactive,ref,nextTick  } from "vue";
import { Search } from '@element-plus/icons-vue';
import { post } from "@/utils/index";
import { ElMessage } from "element-plus";


/**
 * 渲染表格数据
 */
let tableData:TypeAddress[] = reactive([])
/**
 * 搜索框
 */
let refInput = ref<string>('')
/**
 * 搜索请求参数
 */
let refSearch = ref<string>('')
/**
 * 获取回到顶部组件的实例
 */
let refBacktop = ref()
/**
 * 分页的数据
 */
 let addressPagination = reactive(new ClassPagination())


/**
 * 查找地址
 */
function handlerFind (){
    refSearch.value = refInput.value
    addressPagination.page = 1
    getAddressList()
}
/**
 * 分页组件传递过来的数据
 * @param val
 * page:页数
 * limit:条数
 */
 function emitEntProps(val: { page: number, limit: number }) {
    let paging = addressPagination
    paging.page = val.page
    paging.limit = val.limit
    getAddressList()
}

/**
 *  获取地址表格数据
 * 
 */ 
function getAddressList (){
    let data = {
        _gp:'admin.address',
        _mt:'list',
        userId:refSearch.value,
        page:addressPagination.page,
        limit:addressPagination.limit
    }
    post(data).then((res) => {
        if(200 === res.errno){
            let {items,total} = res.data
            tableData.length = 0    //清空
            tableData.push(...items)
            addressPagination.total = total
        }else{
            tableData.length = 0    //清空
            ElMessage.warning(res.errmsg)
        }
    })
}
/**
 * 初次请求
 */
getAddressList()
</script>